package com.hankol.winecellar

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.activity.BaseLazyFragment
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.google.android.material.tabs.TabLayout
import com.hankol.winecellar.databinding.ActivitySaleOrderHomeBinding
import com.hankol.winecellar.fragment.GoodsOrderFragment
import com.hankol.winecellar.view.MyOrderTabVp
import com.hankol.winecellar.viewmodel.WineCellarViewModel

/**新零售二期-销售订单
 * @author: merbng
 * @time: 2022/8/23 12:53
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.SALE_ORDER)
class SaleOrderHomeActivity : BaseActivity(), View.OnClickListener {
    private lateinit var dataBinding: ActivitySaleOrderHomeBinding
    private lateinit var viewModel: WineCellarViewModel
    private val pagerAdapter: MyOrderTabVp by lazy { MyOrderTabVp(supportFragmentManager) }
    private var tabList = mutableListOf<String>("待付款", "待发货", "待收货", "已完成", "全部")
    var orderType = 1
    var dealerCode = ""


    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_sale_order_home
    }

    override fun init() {
        setStatusBarHeight(R.color.color_F9F9F9)
        dataBinding = getDataBinding<ActivitySaleOrderHomeBinding>()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        if (ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_TYPE)[0].checkNotNull()) {
            orderType = ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_TYPE)[0].toInt()
        }
        if (ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_DEALER_CODE)[0].checkNotNull()) {
            dealerCode = ARouterUtils.parseParams(intent.extras!!, Constants.KEY_ORDER_DEALER_CODE)[0]
        }
        pagerAdapter.setTitleList(tabList)
        //-1-全部，0-待付款，1-待发货，2-待收货,5-已完成
        val fragmentList: ArrayList<BaseLazyFragment> = arrayListOf(GoodsOrderFragment.newInstance(0, dealerCode), GoodsOrderFragment.newInstance(1, dealerCode), GoodsOrderFragment.newInstance(2, dealerCode), GoodsOrderFragment.newInstance(5, dealerCode), GoodsOrderFragment.newInstance(-1, dealerCode))
        pagerAdapter.setDatas(fragmentList)

        dataBinding.viewpager.adapter = pagerAdapter
        dataBinding.viewpager.offscreenPageLimit = tabList.size
        dataBinding.tabLayout.setupWithViewPager(dataBinding.viewpager)
        dataBinding.tabLayout.addOnTabSelectedListener(getTabSelectedListener())

        dataBinding.viewpager.currentItem = orderType
        initListener()

    }

    private fun initListener() {
        dataBinding.imgBack.setOnClickListener(this)
        dataBinding.tvOpenSearch.setOnClickListener(this)
    }

    private fun getTabSelectedListener() = object : TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab?) {
            //Tab选中的样式
            setTab(tab)
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {         //非选中时的样式
            tab?.customView = null
        }

        override fun onTabReselected(p0: TabLayout.Tab?) {}
    }

    fun setTab(tab: TabLayout.Tab?) {
        val textView = TextView(this)
        //字体样式
        val selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16f, resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
        textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
        textView.gravity = Gravity.CENTER
        // 选中的字体颜色
        textView.setTextColor(ContextCompat.getColor(this, R.color.color_D53))
        textView.text = tab!!.text
        tab.customView = textView
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.tv_open_search -> {
                val bundle = Bundle()
                bundle.putBoolean(Constants.KEY_SEARCH_ORDER_IS_MANAGER, true)
                ARouterUtils.jump(ActivityRouter.WINE_CELLAR.SEARCH_ORDER, bundle)
            }
        }
    }
}